In a Silverlight program, you’ve seen that a bitmap added to the project as a resource
is bound into the executable. It’s so customary to reference that local
bitmap directly from XAML that very few experienced Silverlight
programmers could tell you offhand how to do it in code. The SilverlightTapToLoad project shows you how.
The SilverlightTapToLoad project contains an Image element in its content grid. The Hello.png bitmap is stored in the Images directory and has a Build Action of Resource.
The MainPage.xaml.cs file requires a using directive for the System.Windows.Media.Imaging namespace for the BitmapImage class. Another using directive for System.Windows.Resources is required for the StreamResourceInfo class.
When the screen is tapped, the event handler accesses the resource using the static GetResourceStream method defined by the Application class:
Example 1. Silverlight Project: SilverlightTapToLoad File: MainPage.xaml.cs
protected override void OnManipulationStarted(ManipulationStartedEventArgs args) { Uri uri = new Uri("/SilverlightTapToLoad;component/Images/Hello.png", UriKind .Relative); StreamResourceInfo resourceInfo = Application.GetResourceStream(uri); BitmapImage bmp = new BitmapImage(); bmp.SetSource(resourceInfo.Stream); img.Source = bmp;
args.Complete(); args.Handled = true; base.OnManipulationStarted(args); }
|
Notice how complicated that
URL is! It begins with the name of the program followed by a semicolon,
followed by the word “component” and then the folder and filename of
the file. If you change the Build Action of the Hello.png file to Content rather than Resource, you can simplify the syntax considerably:
Uri uri = new Uri("Images/Hello.png", UriKind.Relative);
What’s the difference?
Navigate to the Bin/Debug
subdirectory of the Visual Studio project and find the
SilverlightTapToLoad.xap file that contains your program. If you rename
it the file to a ZIP extension you can look inside. The bulk of the file
will be SilverlightTapToLoad.dll, the compiled binary.
In both cases, the bitmap is obviously stored somewhere within the XAP file. The difference is this:
With a Build Action of
Resource for the bitmap, it is stored inside the
SilverlightTapToLoad.dll file along with the compiled program
With a Build
Action of Content, the bitmap is stored external to the
SilverlightTapToLoad.dll file but within the XAP file, and when you
rename the XAP file to a ZIP file, you can see the Images directory and the file.
Which is better?
In a document entitled
“Creating High Performance Silverlight Applications for Windows Phone,”
Microsoft has recommending using a Build
Action of Content rather than Resource for assets included in your
application to minimize the size of the binary and startup time.
However, if these assets are in a Silverlight library that the program
references, then it is better for them to be embedded in the binary with
a Build Action of Resource.
If you have a number of images
in your program, and you don’t want to include them all in the XAP
file, but you’re nervous about downloading the images, why not do a
little of both? Include low resolution (or highly compressed) images in
the XAP file, but download better versions asynchronously while the
application is running.